# This script runs all the analyses for the appendix of the paper.

rm(list=ls())

# Load libraries
library(readstata13)
library(lme4)
library(stargazer)

# Load data
dta_res <- read.dta13("1.1_shallow_commitments_data_main.dta")


##################
# Table A1: Alternative Measure of Democracy (polyarchy in place of polity)
##################

# Define inputs

inputs <- c()

# Model 1: Using the full set of obs, 1982-2015, dem and conc_obs_lag, no interaction
inputs[1] <- "res_withdraw ~ highdem + conc_obs_lag + total + res_events + 
              v2x_gender_diff_rat_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 2: Keep v2x add SOLS, no interaction
inputs[2] <- "res_withdraw ~ highdem + conc_obs_lag + total + res_events + v2x_gender_diff_rat_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 3: Sub v2x with wrindex, no interaction
inputs[3] <- "res_withdraw ~ highdem + conc_obs_lag + total + res_events + wri_diff_rat_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 4: Add the interaction to mod1
inputs[4] <- "res_withdraw ~ highdem + conc_obs_lag + conc_hd + total + res_events + 
            v2x_gender_diff_rat_lag + t_with + t_with2 + 
            t_with3 + (1 | ccode)"

# Model 5: Add the interaction to mod2
inputs[5] <- "res_withdraw ~ highdem + conc_obs_lag + conc_hd + total + res_events + 
              v2x_gender_diff_rat_lag + sols_lag +
              t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 6: Add the interaction to mod3
inputs[6] <- "res_withdraw ~ highdem + conc_obs_lag + conc_hd + total + res_events + 
            wri_diff_rat_lag +
            sols_lag + t_with + t_with2 + 
            t_with3 + (1 | ccode)"

# Run models
mod1 <- glmer(inputs[1], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod1)

mod2 <- glmer(inputs[2], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod2)

mod3 <- glmer(inputs[3], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod3)

mod4 <- glmer(inputs[4], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod4)

mod5 <- glmer(inputs[5], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod5)

mod6 <- glmer(inputs[6], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod6)


# Build Table A1

stargazer(mod1, mod2, mod3, mod4, mod5, mod6,
          out = "table_poly_031520.tex",
          dep.var.labels = "Withdrawing at least one reservation",
          no.space = TRUE,
          keep = c("highdem", "conc_obs_lag", "conc_hd", "v2x_gender_diff_rat_lag",
                   "wri_diff_rat_lag", "sols_lag", "res_events", "total",
                   "t_with", "t_with2", "t_with3", "Constant"),
          order = c("dem", "conc_obs_lag", 
                    "conc_hd", "total",
                    "res_events",
                    "v2x_gender_diff_rat_lag",
                    "wri_diff_rat_lag", "sols_lag", 
                    "t_with", "t_with2", "t_with3", "Constant"),
          keep.stat = c("n", "aic"),
          covariate.labels = c("Democracy (t-1)", "Report (t-1)",
                               "Democracy (t-1) * Report (t-1)",
                               "Initial Reservations", 
                               "Global Withdrawals (t-1)",
                               "Women's Rights (t-1) (V-Dem)", 
                               "Women's Rights (t-1) (CIRI)",
                               "SOLS Change (t-1)",
                               "Time", "Time2", "Time3", "Constant"),
          add.lines = list(c("Years", "1982-2015", "1982-2009", "1982-2005",
                             "1982-2015", "1982-2009", "1982-2005"),
                           c("Countries", "45", "43", "41",
                             "45", "43", "41"),
                           c("Withdrawals", "51", "39", "31", "51",
                             "39", "31")),
          notes.label = "",
          notes = "Standard errors in parentheses",
          font.size = "footnotesize",
          column.sep.width = "0pt")

##################
# Table A2: Objective level of respect for women's rights
##################

inputs <- c()

# Model 1: Using the full set of obs, 1982-2015, dem and conc_obs_lag, no interaction
inputs[1] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + 
              v2x_gender_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 2: Keep v2x add SOLS, no interaction
inputs[2] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + v2x_gender_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 3: Sub v2x with wrindex, no interaction
inputs[3] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + wrindex_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 4: Add the interaction to mod1
inputs[4] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              v2x_gender_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 5: Add the interaction to mod2
inputs[5] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              v2x_gender_lag + sols_lag +
              t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 6: Add the interaction to mod3
inputs[6] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              wrindex_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Run models
mod1 <- glmer(inputs[1], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod1)

mod2 <- glmer(inputs[2], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod2)

mod3 <- glmer(inputs[3], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod3)

mod4 <- glmer(inputs[4], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod4)

mod5 <- glmer(inputs[5], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod5)

mod6 <- glmer(inputs[6], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod6)


# Build Table A2

stargazer(mod1, mod2, mod3, mod4, mod5, mod6,
          out = "table_obj_031520.tex",
          dep.var.labels = "Withdrawing at least one reservation",
          no.space = TRUE,
          keep = c("dem", "conc_dem", "conc_obs_lag", "v2x_gender_lag",
                   "wrindex_lag", "sols_lag", "res_events", "total",
                   "t_with", "t_with2", "t_with3", "Constant"),
          order = c("dem", 
                    "conc_dem", "conc_obs_lag", 
                    "total",
                    "res_events",
                    "v2x_gender_lag",
                    "wrindex_lag", "sols_lag", 
                    "t_with", "t_with2", "t_with3", "Constant"),
          keep.stat = c("n", "aic"),
          covariate.labels = c("Democracy (t-1)", 
                               "Democracy (t-1) * Report (t-1)",
                               "Report (t-1)",
                               "Initial Reservations", 
                               "Global Withdrawals (t-1)",
                               "Women's Rights (t-1) (V-Dem)", 
                               "Women's Rights (t-1) (CIRI)",
                               "SOLS Change (t-1)",
                               "Time", "Time2", "Time3", "Constant"),
          add.lines = list(c("Years", "1982-2015", "1982-2009", "1982-2005",
                             "1982-2015", "1982-2009", "1982-2005"),
                           c("Countries", "46", "44", "42",
                             "46", "44", "42"),
                           c("Withdrawals", "51", "39", "31", "51",
                             "39", "31")),
          notes.label = "",
          notes = "Standard errors in parentheses",
          font.size = "footnotesize",
          column.sep.width = "0pt")

##################
# Table A3: Annual change in respect for women's rights
##################

inputs <- c()

# Model 1: Using the full set of obs, 1982-2015, dem and conc_obs_lag, no interaction
inputs[1] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + 
              v2x_gender_diff_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 2: Keep v2x add SOLS, no interaction
inputs[2] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + v2x_gender_diff_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 3: Sub v2x with wrindex, no interaction
inputs[3] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + wri_diff_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 4: Add the interaction to mod1
inputs[4] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              v2x_gender_diff_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 5: Add the interaction to mod2
inputs[5] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              v2x_gender_diff_lag + sols_lag +
              t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 6: Add the interaction to mod3
inputs[6] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              wri_diff_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Run models
mod1 <- glmer(inputs[1], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod1)

mod2 <- glmer(inputs[2], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod2)

mod3 <- glmer(inputs[3], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod3)

mod4 <- glmer(inputs[4], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod4)

mod5 <- glmer(inputs[5], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod5)

mod6 <- glmer(inputs[6], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod6)


# Build Table A3

stargazer(mod1, mod2, mod3, mod4, mod5, mod6,
          out = "table_change_031520.tex",
          dep.var.labels = "Withdrawing at least one reservation",
          no.space = TRUE,
          keep = c("dem", "conc_dem", "conc_obs_lag", "v2x_gender_diff_lag",
                   "wri_diff_lag", "sols_lag", "res_events", "total",
                   "t_with", "t_with2", "t_with3", "Constant"),
          order = c("dem", "conc_dem",
                    "conc_obs_lag", 
                    "total",
                    "res_events",
                    "v2x_gender_diff_lag",
                    "wri_diff_lag", "sols_lag", 
                    "t_with", "t_with2", "t_with3", "Constant"),
          keep.stat = c("n", "aic"),
          covariate.labels = c("Democracy (t-1)", 
                               "Democracy (t-1) * Report (t-1)",
                               "Report (t-1)",
                               "Initial Reservations", 
                               "Global Withdrawals (t-1)",
                               "Women's Rights (t-1) (V-Dem)", 
                               "Women's Rights (t-1) (CIRI)",
                               "SOLS Change (t-1)",
                               "Time", "Time2", "Time3", "Constant"),
          add.lines = list(c("Years", "1982-2015", "1982-2009", "1982-2005",
                             "1982-2015", "1982-2009", "1982-2005"),
                           c("Countries", "46", "44", "42",
                             "46", "44", "42"),
                           c("Withdrawals", "51", "39", "31", "51",
                             "39", "31")),
          notes.label = "",
          notes = "Standard errors in parentheses",
          font.size = "footnotesize",
          column.sep.width = "0pt")


##################
# Table A4: Control for Articles 2 & 16
##################

inputs <- c()

# Model 1: Using the full set of obs, 1982-2015, dem and conc_obs_lag, no interaction
inputs[1] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + art2_16 +
              v2x_gender_diff_rat_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 2: Keep v2x add SOLS, no interaction
inputs[2] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + v2x_gender_diff_rat_lag +
              art2_16 + sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 3: Sub v2x with wrindex, no interaction
inputs[3] <- "res_withdraw ~ dem + conc_obs_lag + total + res_events + wri_diff_rat_lag +
              art2_16 + sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 4: Add the interaction to mod1
inputs[4] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              art2_16 + v2x_gender_diff_rat_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 5: Add the interaction to mod2
inputs[5] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              art2_16 + v2x_gender_diff_rat_lag + sols_lag +
              t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Model 6: Add the interaction to mod3
inputs[6] <- "res_withdraw ~ dem + conc_obs_lag + conc_dem + total + res_events + 
              art2_16 + wri_diff_rat_lag +
              sols_lag + t_with + t_with2 + 
              t_with3 + (1 | ccode)"

# Run models
mod1 <- glmer(inputs[1], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod1)

mod2 <- glmer(inputs[2], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod2)

mod3 <- glmer(inputs[3], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod3)

mod4 <- glmer(inputs[4], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod4)

mod5 <- glmer(inputs[5], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod5)

mod6 <- glmer(inputs[6], data = dta_res, family = binomial(link = "logit"), nAGQ = 0, 
              control = glmerControl(optimizer = "nloptwrap"))
# summary(mod6)


# Build Table A4

stargazer(mod1, mod2, mod3, mod4, mod5, mod6,
          # out = "table_art216_031520.tex",
          dep.var.labels = "Withdrawing at least one reservation",
          no.space = TRUE,
          keep = c("dem", "conc_dem", "conc_obs_lag", "v2x_gender_diff_rat_lag",
                   "wri_diff_rat_lag", "sols_lag", "res_events", "total", "art2_16",
                   "t_with", "t_with2", "t_with3", "Constant"),
          order = c("dem", "conc_dem", 
                    "conc_obs_lag", 
                    "total",
                    "res_events",
                    "art2_16",
                    "v2x_gender_diff_rat_lag",
                    "wri_diff_rat_lag", "sols_lag", 
                    "t_with", "t_with2", "t_with3", "Constant"),
          keep.stat = c("n", "aic"),
          covariate.labels = c("Democracy (t-1)",
                               "Democracy (t-1) * Report (t-1)",
                               "Report (t-1)",
                               "Initial Reservations", 
                               "Global Withdrawals (t-1)",
                               "Articles 2 or 16",
                               "Women's Rights (t-1) (V-Dem)", 
                               "Women's Rights (t-1) (CIRI)",
                               "SOLS Change (t-1)",
                               "Time", "Time2", "Time3", "Constant"),
          add.lines = list(c("Years", "1982-2015", "1982-2009", "1982-2005",
                             "1982-2015", "1982-2009", "1982-2005"),
                           c("Countries", "45", "43", "41",
                             "45", "43", "41"),
                           c("Withdrawals", "51", "39", "31", "51",
                             "39", "31")),
          notes.label = "",
          notes = "Standard errors in parentheses",
          font.size = "footnotesize",
          column.sep.width = "0pt")
